package com.nettools;

import com.fileoperation.model.LogOutEvent;
import com.fileoperation.user.UserInfo;
import com.fileoperation.user.UserInfoParser;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Response;

/**
 * Created by Administrator on 2016-12-21.
 */

public class NetUtils {
    private static NetUtils instance;
    private HttpUtils httpUtils;

    private NetUtils(){
        httpUtils = new HttpUtils();
    }


    public static NetUtils getInstance(){
        if (instance == null){
            synchronized (NetUtils.class){
                instance = new NetUtils();
            }
        }
        return instance;
    }


    public static void requestRequestIdentifyingCode(String phoneNumber){
        System.out.println("我很强！");
    }


    /**
     * 刷新access_token
     * @return
     * @throws IOException
     */
    public void refreshAccessToken() throws IOException, JSONException {
        Map<String, Object> params = new HashMap<>(1);
        params.put("refresh_token", UserInfo.getInstance().getRefreshToken());
        Response response = instance.httpUtils.postFormSync(params, Constants.REFRESH_TOKEN);
        JSONObject jsonResponse = new JSONObject(response.body().string());
        if (jsonResponse.optBoolean(Constants.STATUS)){
            UserInfoParser.parseUserInfo(jsonResponse.optJSONObject(Constants.DATA));
        }else {
            System.out.println("发送登出事件！");
            EventBus.getDefault().post(new LogOutEvent());
        }
    }


    /**
     * 登录
     * @param username
     * @param password
     * @return
     * @throws IOException
     */
    public Response logIn(String username, String password) throws IOException {
        Map<String, Object> params = new HashMap<>(2);
        params.put("username", username);
        params.put("password", password);
        params.put("action", Constants.LOGIN);
        Response response = instance.httpUtils.postFormSync(params, Constants.BASE_URL);
        return response;
    }

    public Response getOrderInfo(String orderId) throws IOException {
        Map<String, Object> params = new HashMap<>(2);
        params.put("Order_Number", orderId);
        params.put("action", Constants.GET_ORDER_INFO);
        Response response = instance.httpUtils.postFormSync(params, Constants.BASE_URL);
        return response;
    }

    public Response sendOrderPrice(String orderId,String price) throws IOException {
        Map<String, Object> params = new HashMap<>(2);
        params.put("Order_Number", orderId);
        params.put("Discount", price);

        params.put("action", Constants.SEND_PRICE_INFO);
        Response response = instance.httpUtils.postFormSync(params, Constants.BASE_URL);
        return response;
    }

    /**
     * 获得
     * @param content
     * @return
     * @throws IOException
     */
    public Response getSignData(String content) throws IOException {
        Map<String, Object> params = new HashMap<>(1);
        params.put("content", content);
        Response response = instance.httpUtils.postFormSync(params, Constants.GET_SIGN_DATA);
        return response;
    }


    /**
     * 向服务器通知上传结果
     * @param size
     * @param path
     * @return
     * @throws IOException
     */
    public Response sendUploadConsequence(int size, String path) throws IOException {
        Map<String, Object> params = new HashMap<>(2);
        params.put("size", size);
        params.put("path", path);
        Response response = instance.httpUtils.postFormSync(params, Constants.SEND_UPLOAD_CONSEQUENCE);
        return response;
    }
}
